package com.wdc.deliveryrobot.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.sirui.selfstudysdk.main.SelfChassisState;
import com.wdc.deliveryrobot.R;
import com.wdc.deliveryrobot.control.DataController;
import com.wdc.deliveryrobot.dialog.HintDialog;
import com.wdc.deliveryrobot.utils.TimerTaskManager;

import org.greenrobot.eventbus.EventBus;

public abstract class BaseActivity extends AppCompatActivity {

    public SelfChassisState selfChassisState;

    public DataController controller;

    public TimerTaskManager timerTaskManager;

    public static boolean init = false; // 静态变量，确保只初始化一次

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        controller = DataController.getInstance();
        timerTaskManager = TimerTaskManager.getInstance(this);
        selfChassisState = controller.selfChassisState;
        hideSystemUI();



        // 启动定时器
        timerTaskManager.startTimer();
        //initSdk();
    }

    @Override
    protected void onStop() {
        super.onStop();
        hideSystemUI();
    }

    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
        hideSystemUI();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    protected void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        decorView.setSystemUiVisibility(uiOptions);

        // 使 ActionBar 隐藏
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }

    public void initSdk(){
        if (!init){
            selfChassisState = SelfChassisState.getInstance();
            init = selfChassisState.init("192.168.31.7:9090");
            Log.d("thd_","连接成功");
            if (!init){
                HintDialog hintDialog = new HintDialog(getBaseContext());
                hintDialog.setMessage(getString(R.string.tcp_connect_error));

                hintDialog.setCancelButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        hintDialog.dismiss();
                    }
                });

                hintDialog.setOkButtonClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        initSdk();
                    }
                });
                hintDialog.show();
            }
        }
    }

}
